<?php
/**
 * Created by PhpStorm.
 * User: 17464
 * Date: 2019/12/24
 * Time: 16:29
 */

namespace App\Http\Controllers\Api;

use App\Http\Requests\Setting\AliSettingRequest;
use App\Http\Requests\Setting\AuctionSettingRequest;
use App\Http\Requests\Setting\GetAliSettingRequest;
use App\Http\Requests\Setting\GetAuctionSettingRequest;
use App\Http\Requests\Setting\GetOssSettingRequest;
use App\Http\Requests\Setting\GetWeChatSettingRequest;
use App\Http\Requests\Setting\OssSettingRequest;
use App\Http\Requests\Setting\WeChatSettingRequest;
use App\Repositories\SettingRepository;
use App\Repositories\SystemRepository;
use App\Traits\Setting;
use App\Transformers\System\GetFailedJobsPageTransformer;
use Illuminate\Http\Request;

/**
 * Class SystemController
 * @package App\Http\Controllers\Api
 */
class SystemController extends BaseAdminController
{
    use Setting;

    /**
     * @var SystemRepository 注入的数据模型
     */
    protected $systemRepository;

    /**
     * @var SettingRepository 注入的外部服务
     */
    protected $settingRepository;

    /**
     * SystemController constructor.
     * @param SystemRepository $systemRepository
     * @param SettingRepository $settingRepository
     */
    public function __construct(SystemRepository $systemRepository,
                                SettingRepository $settingRepository)
    {
        $this->systemRepository = $systemRepository;
        $this->settingRepository = $settingRepository;
    }


    /**
     * 微信配置保存
     * 配置文件配置和修稿，涉及到后台和小程序api
     * 1、后台在Redis存储数据，小程序api进行读取
     * 2、后台在缓存中存储数据，小程序api进行读取
     * 3、后台保存在数据库中，并缓存、或存储在Redis中，小程序api进行读取
     * 4、后台直接读取Linux目录文件进行修改
     * @param WeChatSettingRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function weChatSetting(WeChatSettingRequest $request){
        $all = $request->all(['key', 'app_id', 'app_secret', 'mch_id', 'keySign', 'notify_url']);

        $value = [];
        data_set($value, 'app_id', $all['app_id']);
        data_set($value, 'app_secret', $all['app_secret']);
        data_set($value['pay'], 'mch_id', $all['mch_id']);
        data_set($value['pay'], 'key', $all['keySign']);
        data_set($value['pay'], 'notify_url', $all['notify_url']);

        $value = \GuzzleHttp\json_encode($value);
        $this->settingRepository->saveSettingRepository(WECHAT_KEY, $value);

        //redis缓存数据
        $this->storeSettingData(WECHAT_KEY, $value);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取微信配置
     * @param GetWeChatSettingRequest $request
     * @return string
     */
    public function getWeChatSetting(GetWeChatSettingRequest $request){
        $key = $request->get('key');

        //从Redis获取，暂时不从数据库获取
        if($value = $this->getSettingCache(WECHAT_KEY)){
            $value = \GuzzleHttp\json_decode($value);

            data_set($value, 'key', $key);

            return  \GuzzleHttp\json_encode(['data'=>$value]);
        }

        $this->response->errorBadRequest('请设置微信配置，否则无法使用相关小程序！');
    }

    /**
     * 保存阿里第三方配置
     * @param AliSettingRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function aliSetting(AliSettingRequest $request){
        $all = $request->all(['key', 'ship_host', 'app_code', 'ip_host', 'real_host']);

        $value = [];
        data_set($value, 'app_code', $all['app_code']);
        data_set($value, 'ship_host', $all['ship_host']);
        data_set($value, 'ip_host', $all['ip_host']);
        data_set($value, 'real_host', $all['real_host']);

        $value = \GuzzleHttp\json_encode($value);
        $this->settingRepository->saveSettingRepository(ALI_KEY, $value);

        //redis缓存数据
        $this->storeSettingData(ALI_KEY, $value);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取阿里第三方配置
     * @param GetAliSettingRequest $request
     * @return string
     */
    public function getAliSetting(GetAliSettingRequest $request){
        $key = $request->get('key');

        //从Redis获取，暂时不从数据库获取
        if($value = $this->getSettingCache(ALI_KEY)){
            $value = \GuzzleHttp\json_decode($value);

            data_set($value, 'key', $key);

            return  \GuzzleHttp\json_encode(['data'=>$value]);
        }

        $this->response->errorBadRequest('请设置物流、实名认证等配置，否则无法使用阿里第三方服务！');
    }

    /**
     * 保存与订单和拍卖有关的配置参数
     * @param AliSettingRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function auctionSetting(AuctionSettingRequest $request){
        $all = $request->all(['key', 'order_ttl', 'min_withdraw_money', 'min_recharge_money']);

        $value = [];
        data_set($value, 'order_ttl', data_get($value, 'order_ttl', 30));
        data_set($value, 'min_withdraw_money', data_get($value, 'min_withdraw_money', 100));
        data_set($value, 'min_recharge_money', data_get($value, 'min_recharge_money', 1));

        $value = \GuzzleHttp\json_encode($value);
        $this->settingRepository->saveSettingRepository(AUCTION_KEY, $value);

        //redis缓存数据
        $this->storeSettingData(AUCTION_KEY, $value);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取与订单和拍卖有关的配置参数
     * @param GetAuctionSettingRequest $request
     * @return \Dingo\Api\Http\Response|string
     */
    public function getAuctionSetting(GetAuctionSettingRequest $request){
        $key = $request->get('key');

        //从Redis获取，暂时不从数据库获取
        if($value = $this->getSettingCache(AUCTION_KEY)){
            $value = \GuzzleHttp\json_decode($value);

            data_set($value, 'key', $key);

            return  \GuzzleHttp\json_encode(['data'=>$value]);
        }

        return $this->response->accepted('', '');
    }

    /**
     * 获取失败队列
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getFailedJobsPage(Request $request){
        $limit = $request->get('limit');
        $data = $request->all();
        $page = $this->systemRepository->getFailedJobsPageRepository($data, $limit);

        return $this->response->paginator($page, new GetFailedJobsPageTransformer());
    }

    /**
     * 保存OSS配置
     * @param OssSettingRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function ossSetting(OssSettingRequest $request){
        $all = $request->all(['key', 'AccessKeyID', 'AccessKeySecret', 'RoleArn',
            'BucketName', 'Endpoint', 'Domain', 'TokenExpireTime']);

        $value = [];
        data_set($value, 'AccessKeyID', $all['AccessKeyID']);
        data_set($value, 'AccessKeySecret', $all['AccessKeySecret']);
        data_set($value, 'RoleArn', $all['RoleArn']);
        data_set($value, 'BucketName', $all['BucketName']);
        data_set($value, 'Endpoint', $all['Endpoint']);
        data_set($value, 'Domain', $all['Domain']);
        data_set($value, 'TokenExpireTime', $all['TokenExpireTime']);

        $value = \GuzzleHttp\json_encode($value);
        $this->settingRepository->saveSettingRepository(OSS_KEY, $value);

        //redis缓存数据
        $this->storeSettingData(OSS_KEY, $value);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取OSS配置参数
     * @param GetOssSettingRequest $request
     * @return \Dingo\Api\Http\Response|string
     */
    public function getOssSetting(GetOssSettingRequest $request){
        $key = $request->get('key');

        //从Redis获取，暂时不从数据库获取
        if($value = $this->getSettingCache(OSS_KEY)){
            $value = \GuzzleHttp\json_decode($value);

            data_set($value, 'key', $key);

            return  \GuzzleHttp\json_encode(['data'=>$value]);
        }

        return $this->response->accepted('', '');
    }
}
